/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.EstPriceBean;
import com.minierp.model.EstPrice;
import java.math.BigDecimal;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class EstPriceValidator implements Validator {
	public boolean supports(Class clazz) {
		return EstPriceBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		EstPriceBean bean = (EstPriceBean) obj;
		EstPrice form = bean.getEstPrice();
		boolean hasError = false;
		if (form.getPriceType().equals("EST") && form.getEstId() == 0L) {
			hasError = true;
			String e = "";
			if (form.getEstType().equals("CLIENT")) {
				e = "客户";
			} else if (form.getEstType().equals("PROVIDER")) {
				e = "供应商";
			} else if (form.getEstType().equals("COOPERATOR")) {
				e = "外协商";
			}

			errors.rejectValue("estPrice.estId", "请选择『" + e + "』");
		}

		if (!hasError
				&& (form.getProName() == null || form.getProName().equals(""))
				&& form.getMakeTypeId() == 0L) {
			errors.rejectValue("estPrice.proName", "", "请选择『项目类型』或输入『材质』");
			hasError = true;
		}

		if (!hasError && form.getLength() != null
				&& !form.getLength().equals("")) {
			try {
				new BigDecimal(form.getLength());
			} catch (Exception arg8) {
				errors.rejectValue("estPrice.length", "", "『宽』只能输入数字");
				hasError = true;
			}
		}

		if (!hasError && form.getWidth() != null && !form.getWidth().equals("")) {
			try {
				new BigDecimal(form.getWidth());
			} catch (Exception arg7) {
				errors.rejectValue("estPrice.width", "", "『高』只能输入数字");
				hasError = true;
			}
		}

		if (!hasError && form.getMakeRequire() != null
				&& form.getMakeRequire().length() >= 5000) {
			hasError = true;
			errors.rejectValue("estPrice.makeRequire", "", "『制作要求』不能超过5000个字符");
		}

		if (!hasError) {
			try {
				form.setPrice(new BigDecimal(bean.getPriceStr()));
			} catch (Exception arg6) {
				hasError = true;
				errors.rejectValue("priceStr", "", "『单价』只能输入数字");
			}
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 100) {
			hasError = true;
			errors.rejectValue("estPrice.mark", "", "『备注』不能超过100个字符");
		}

	}
}